package com.bt.mes.brm.provider.service;

import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISMTR_NOT_FOUND_MTRL;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISQRS_QRS_ALREADY_EXIST;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.bt.mes.brm.api.service.IXpbismtrService;
import com.bt.mes.brm.provider.tx.Xpbismtr.XpbismtrI;
import com.bt.mes.brm.provider.tx.Xpbismtr.XpbismtrIA;
import com.bt.mes.brm.provider.tx.Xpbismtr.XpbismtrO;
import com.bt.mes.brm.provider.tx.Xpbismtr.XpbismtrOA;
import com.bt.mes.common.dao.IBisMtrlDao;
import com.bt.mes.common.util.MqOpe;
import com.bt.mes.common.util.Util;
import com.bt.table.ppt.Bis_mtrl;
import com.bt.table.ppt.Ret_wh_mtrl;

@Service("xpbismtrService")
public class XpbismtrService implements IXpbismtrService {
	private Logger logger = Logger.getLogger(XpbismtrService.class);
	@Autowired
	private XpbismtrI inTrx;

	@Autowired
	private XpbismtrO outTrx;

	@Autowired
	private IBisMtrlDao bisMtrlDao;

	@Autowired
	private MqOpe mqOpe;

	public String subMainProc(String strInMsg) {
		logger.info("InTrx is " + strInMsg);
		Session session = bisMtrlDao.getCrSession();
		try {
			outTrx.setTrx_id("XPBISMTR");
			outTrx.setType_id("O");
			inTrx = (XpbismtrI) mqOpe.inStr2Obj(strInMsg, inTrx);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByCondition();
				break;
			case ACTION_FLG_ADD:
				addFnc();
				break;
			case ACTION_FLG_UPDATE:
				updateFnc();
				break;
			case ACTION_FLG_DELETE:
				deleteFnc();
				break;
			case 'G':
				queryformkr();
				break;
			case 'F':
				queryfordsc();
				break;
			default:
				errActionFnc();
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {

			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			logger.info(mqOpe.obj2Str(outTrx));
		}
		return mqOpe.obj2Str(outTrx);
	}

	public void addFnc() throws Exception {

		XpbismtrIA iary = inTrx.getIary().get(0);
		String mtrl_product_id = iary.getMtrl_prod_id();

		Bis_mtrl queryBisQrs = bisMtrlDao.get(mtrl_product_id);
		if (queryBisQrs != null) {
			outTrx.setRtn_code(E_XPBISQRS_QRS_ALREADY_EXIST + "");
			outTrx.setRtn_mesg("Record already Exist");
			return;
		}
		Bis_mtrl bis_mtrl = new Bis_mtrl();
		bis_mtrl.setMtrl_prod_id(mtrl_product_id);
		bis_mtrl.setMtrl_prod_dsc(iary.getMtrl_prod_dsc());
		bis_mtrl.setMtrl_cate(iary.getMtrl_cate());
		bis_mtrl.setMtrl_mkr(iary.getMtrl_mkr());
		bis_mtrl.setMtrl_unit(iary.getMtrl_unit());
		bis_mtrl.setMtrl_prod_id_len(iary.getMtrl_prod_id_len());
		bis_mtrl.setMtrl_chg_cate(iary.getMtrl_chg_cate());
		bis_mtrl.setMtrl_use_mode(iary.getMtrl_use_mode());
		bis_mtrl.setMtrl_part(iary.getMtrl_part());
		bisMtrlDao.save(bis_mtrl);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");

	}

	public XpbismtrO updateFnc() {

		XpbismtrIA iary = inTrx.getIary().get(0);
		String mtrl_product_id = iary.getMtrl_prod_id();

		Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_product_id);
		if (bis_mtrl == null) {
			outTrx.setRtn_code(E_XPBISMTR_NOT_FOUND_MTRL + "");
			outTrx.setRtn_mesg("Record already Exist");
			return outTrx;
		}
		if (iary.getMtrl_prod_dsc() != null) {
			bis_mtrl.setMtrl_prod_dsc(iary.getMtrl_prod_dsc());
		}
		if (iary.getMtrl_cate() != null) {
			bis_mtrl.setMtrl_cate(iary.getMtrl_cate());
		}
		if (iary.getMtrl_mkr() != null) {
			bis_mtrl.setMtrl_mkr(iary.getMtrl_mkr());
		}
		if (iary.getMtrl_unit() != null) {
			bis_mtrl.setMtrl_unit(iary.getMtrl_unit());
		}
		if (iary.getMtrl_prod_id_len() != null) {
			bis_mtrl.setMtrl_prod_id_len(iary.getMtrl_prod_id_len());
		}
		if (iary.getMtrl_chg_cate() != null) {
			bis_mtrl.setMtrl_chg_cate(iary.getMtrl_chg_cate());
		}
		if (iary.getMtrl_use_mode() != null) {
			bis_mtrl.setMtrl_use_mode(iary.getMtrl_use_mode());
		}
		if (iary.getMtrl_part() != null) {
			bis_mtrl.setMtrl_part(iary.getMtrl_part());
		}
		bisMtrlDao.update(bis_mtrl);

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return outTrx;
	}

	public XpbismtrO deleteFnc() {

		XpbismtrIA iary = inTrx.getIary().get(0);

		Bis_mtrl bis_mtrl = bisMtrlDao.get(iary.getMtrl_prod_id());
		if (bis_mtrl == null) {
			outTrx.setRtn_code(E_XPBISMTR_NOT_FOUND_MTRL + "");
			outTrx.setRtn_mesg("not found record");
			return outTrx;
		}
		bisMtrlDao.delete(bis_mtrl);

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return outTrx;

	}

	public XpbismtrO queryByCondition() {

		List<XpbismtrOA> oaryList = new ArrayList<XpbismtrOA>();

		XpbismtrIA iary = inTrx.getIary().get(0);
		String mtrl_prod_id = iary.getMtrl_prod_id();

		String hql = "FROM Bis_mtrl where 1=1 ";
		if (iary.getMtrl_prod_id() != null) {
			if (mtrl_prod_id.indexOf('*') == -1) {
				hql = hql + " and mtrl_prod_id='" + mtrl_prod_id + "'";
			} else {
				hql = hql + " and mtrl_prod_id like '" + mtrl_prod_id.replace('*', '%') + "'";
			}
		}
		if (iary.getMtrl_cate() != null) {
			hql = hql + " and mtrl_cate='" + iary.getMtrl_cate() + "'";
		}
		if (iary.getMtrl_mkr() != null) {
			hql = hql + " and mtrl_mkr='" + iary.getMtrl_mkr() + "'";
		}
		List<Bis_mtrl> bisMtrlList = bisMtrlDao.find(hql);
		for (Bis_mtrl bis_mtrl : bisMtrlList) {
			XpbismtrOA oary = new XpbismtrOA();
			oary.setMtrl_prod_id(bis_mtrl.getMtrl_prod_id());
			oary.setMtrl_prod_dsc(bis_mtrl.getMtrl_prod_dsc());
			oary.setMtrl_cate(bis_mtrl.getMtrl_cate());
			oary.setMtrl_mkr(bis_mtrl.getMtrl_mkr());
			oary.setMtrl_unit(bis_mtrl.getMtrl_unit());
			oary.setMtrl_prod_id_len(bis_mtrl.getMtrl_prod_id_len());
			oary.setMtrl_chg_cate(bis_mtrl.getMtrl_chg_cate());
			oary.setMtrl_use_mode(bis_mtrl.getMtrl_use_mode());
			oary.setMtrl_part(bis_mtrl.getMtrl_part());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return outTrx;

	}

	public XpbismtrO queryformkr() {

		List<XpbismtrOA> oaryList = new ArrayList<XpbismtrOA>();

		XpbismtrIA iary = inTrx.getIary().get(0);
		String mtrl_mkr = iary.getMtrl_mkr();
		String mtrl_cate = iary.getMtrl_cate();
		String mtrl_prod_dsc = iary.getMtrl_prod_dsc();
		String sql = "SELECT DISTINCT MTRL_PROD_ID,A.* FROM BIS_MTRL A JOIN BIS_DATA B ON A.MTRL_PROD_ID=B.EXT_1 WHERE 1=1";

		if (!Util.isSpaceCheck(mtrl_mkr)) {
			sql = sql + " AND B.DATA_CATE='CSMT' AND B.DATA_ITEM='" + mtrl_mkr + "'";
		}
		if (!Util.isSpaceCheck(mtrl_cate)) {
			sql = sql + " AND A.MTRL_CATE='" + mtrl_cate + "'";
		}
		if (!Util.isSpaceCheck(mtrl_prod_dsc)) {
			sql = sql + " AND A.MTRL_PROD_DSC LIKE '%" + mtrl_prod_dsc + "%'";
		}

		List<Bis_mtrl> bisMtrlList = bisMtrlDao.findBySQL(sql, Bis_mtrl.class);
		for (Bis_mtrl bis_mtrl : bisMtrlList) {
			XpbismtrOA oary = new XpbismtrOA();
			oary.setMtrl_prod_id(bis_mtrl.getMtrl_prod_id());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return outTrx;
	}

	public XpbismtrO queryfordsc() {

		List<XpbismtrOA> oaryList = new ArrayList<XpbismtrOA>();

		XpbismtrIA iary = inTrx.getIary().get(0);
		String mtrl_mkr = iary.getMtrl_mkr();
		String mtrl_cate = iary.getMtrl_cate();
		String mtrl_prod_dsc = iary.getMtrl_prod_dsc();
		String dest_shop = iary.getDest_shop();

		String sql = "";
		if (mtrl_prod_dsc != null) {
			sql = "SELECT A.* FROM RET_WH_MTRL A JOIN BIS_MTRL B ON A.MTRL_PROD_ID_FK=B.MTRL_PROD_ID WHERE A.CUS_ID_FK='"
					+ mtrl_mkr + "' AND A.MTRL_CATE='" + mtrl_cate + "' AND A.DEST_SHOP='" + dest_shop
					+ "' AND B.MTRL_PROD_DSC LIKE '%" + mtrl_prod_dsc + "%'";
		} else {
			sql = "SELECT * FROM RET_WH_MTRL WHERE CUS_ID_FK='" + mtrl_mkr + "' AND MTRL_CATE='" + mtrl_cate
					+ "' AND DEST_SHOP='" + dest_shop + "'";
		}

		List<Ret_wh_mtrl> retWhMtrlList = bisMtrlDao.findBySQL(sql, Ret_wh_mtrl.class);
		for (Ret_wh_mtrl ret_wh_mtrl : retWhMtrlList) {
			XpbismtrOA oary = new XpbismtrOA();
			oary.setMtrl_prod_id(ret_wh_mtrl.getMtrl_prod_id_fk());
			oary.setMtrl_cate(ret_wh_mtrl.getMtrl_cate());
			oary.setMtrl_mkr(ret_wh_mtrl.getCus_id_fk());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return outTrx;
	}

	public XpbismtrO errActionFnc() {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
		return outTrx;
	}

}
